﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Text;

namespace NMemcached.Client.Configuration
{
    internal class ServerElement : ConfigurationElement
    {
        private const string HostOrIPKey = "server";
        private const string PortKey = "port";
        public const string ProtocolKey = "protocol";
        public const string TransportKey = "transport";
        public const string UseCompressionKey = "useCompression";
        public const string CompressionAlgorithmKey = "compressionAlgorithm";
        public const string PayloadEncodingKey = "payloadEncoding";
        public const string ProtocolEncodingKey = "protocolEncoding";
        public const string ReceiveTimeoutKey = "receiveTimeout";
        public const string SendTimeoutKey = "sendTimeout";

        [ConfigurationProperty(ServerElement.HostOrIPKey, IsRequired = true)]
        public string HostOrIP
        {
            get
            {
                return (string)this[ServerElement.HostOrIPKey];
            }
            set
            {
                this[ServerElement.HostOrIPKey] = value;
            }
        }

        [ConfigurationProperty(ServerElement.PortKey, DefaultValue = 11211, IsRequired = false)]
        [IntegerValidator(ExcludeRange = false, MinValue = 1, MaxValue = 65535)]
        public int Port
        {
            get
            {
                return (int)this[ServerElement.PortKey];
            }
            set
            {
                this[ServerElement.PortKey] = value;
            }
        }

        [ConfigurationProperty(ServerElement.ProtocolKey, DefaultValue = ConnectionType.Text, IsRequired = false)]
        [TypeConverter(typeof(ProtocolConverter))]
        public ConnectionType Protocol
        {
            get
            {
                return (ConnectionType)this[ServerElement.ProtocolKey];
            }
            set
            {
                this[ServerElement.ProtocolKey] = value;
            }
        }

        [ConfigurationProperty(ServerElement.TransportKey, DefaultValue = TransportType.Tcp, IsRequired = false)]
        [TypeConverter(typeof(TransportConverter))]
        public TransportType Transport
        {
            get
            {
                return (TransportType)this[ServerElement.TransportKey];
            }
            set
            {
                this[ServerElement.TransportKey] = value;
            }
        }

        [ConfigurationProperty(ServerElement.UseCompressionKey, DefaultValue = false, IsRequired = false)]
        public bool UseCompression
        {
            get
            {
                return (bool)this[ServerElement.UseCompressionKey];
            }
            set
            {
                this[ServerElement.UseCompressionKey] = value;
            }
        }

        [ConfigurationProperty(ServerElement.CompressionAlgorithmKey, DefaultValue = "GZip", IsRequired = false)]
        public CompressionAlgorithm Compressor
        {
            get
            {
                return (CompressionAlgorithm)this[ServerElement.CompressionAlgorithmKey];
            }
            set
            {
                this[ServerElement.CompressionAlgorithmKey] = value.ToString().ToLower();
            }
        }

        [ConfigurationProperty(ServerElement.PayloadEncodingKey, DefaultValue = "UTF8", IsRequired = false)]
        public EncodingType PayloadEncoding
        {
            get
            {
                return (EncodingType)this[ServerElement.PayloadEncodingKey];
            }
            set
            {
                this[ServerElement.PayloadEncodingKey] = value;
            }
        }

        [ConfigurationProperty(ServerElement.ProtocolEncodingKey, DefaultValue = "UTF8", IsRequired = false)]
        public EncodingType ProtocolEncoding
        {
            get
            {
                return (EncodingType)this[ServerElement.ProtocolEncodingKey];
            }
            set
            {
                this[ServerElement.ProtocolEncodingKey] = value;
            }
        }

        [ConfigurationProperty(ServerElement.ReceiveTimeoutKey, DefaultValue = "5000", IsRequired = false)]
        public int ReceiveTimeout
        {
            get
            {
                return (int)this[ServerElement.ReceiveTimeoutKey];
            }
            set
            {
                this[ServerElement.ReceiveTimeoutKey] = value;
            }
        }

        [ConfigurationProperty(ServerElement.SendTimeoutKey, DefaultValue = "5000", IsRequired = false)]
        public int SendTimeout
        {
            get
            {
                return (int)this[ServerElement.SendTimeoutKey];
            }
            set
            {
                this[ServerElement.SendTimeoutKey] = value;
            }
        }

        public ServerElement()
            : base()
        {
        }
    }
}
